package com.example.springboot.service;

import cn.hutool.core.date.DateUtil;
import com.example.springboot.common.PetEnum;
import com.example.springboot.common.config.JwtTokenUtils;
import com.example.springboot.dao.PetDao;
import com.example.springboot.entity.Account;
import com.example.springboot.entity.Pet;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PetService {
    @Resource
    PetDao petDao;

    public List<Pet> selectAll() {
        Pet  pet=new Pet();
        Account account=JwtTokenUtils.getCurrentUser();
        if (account.getRole().equals("user")){
            pet.setUserId(account.getId());
            pet.setState(PetEnum.NO_FOSTER.state);
        }
        return petDao.selectAll(pet);
    }

    public void updateById(Pet pet) {
        petDao.updateById(pet);
    }

    public PageInfo<Pet> selectPage(Pet pet, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Pet> petList = petDao.selectAll(pet);
        return PageInfo.of(petList);
    }

    public void savePet(Pet pet) {
        Account account= JwtTokenUtils.getCurrentUser();
        pet.setUserId(account.getId());
        petDao.insertPet(pet);
    }

    public void deleteById(Integer id) {
        petDao.deleteById(id);
    }



}